package priv.conceit.sc.hoxton.file.auth;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;
import priv.conceit.sc.hoxton.common.entity.auth.authority.dto.CurrentUserInfoDTO;
import priv.conceit.sc.hoxton.common.web.security.TokenUtil;
import priv.conceit.sc.hoxton.common.web.security.WebUserDetail;
import priv.conceit.sc.hoxton.interior.client.AuthClient;


/**
 * 用户信息获取
 *
 * @author conceit
 * @date 2020/11/11 10:08
 */
@Component
@Slf4j
public class WebUserDetailServiceImpl implements UserDetailsService {


	@Autowired
	private TokenUtil tokenUtil;

	@Autowired
	private AuthClient authClient;


	/**
	  * loadUserByUsername
	  *
	  * @param token
	  * @return: org.springframework.security.core.userdetails.UserDetails
	  * @author: conceit
	  * @date: 2019/9/20 19:02
	  */
	@Override
	public UserDetails loadUserByUsername(String token) throws AuthenticationException {
		CurrentUserInfoDTO userInfo=this.authClient.authorityUserTokenVerify(token).getResult();
		WebUserDetail webUserDetail = (WebUserDetail) tokenUtil.userInfo2UserDetails(userInfo);
		return webUserDetail;
	}

}
